# /bin/bash
rm -rf output
mkdir output
mkdir output/log
mkdir output/result
cur_path=$(pwd)
flag=0

function check_result {
        local num
        local target_num
        local op_name
        log_file=$1
        op_name=$2
        num=$( cat ${log_file} | grep 'fail' | wc -l)
        err_num=$( cat ${log_file} | grep 'retcode' | wc -l)
        errr_num=$( cat ${log_file} | grep -i 'error' | wc -l)
        if [[ $num == 0 ]] && [[ $err_num == 0 ]] && [[ $errr_num == 0 ]]
        then
                echo "$op_name pass"
				flag=$[flag+1]
        else
                echo "$op_name fail"
        fi
}

echo "===init==="
cd /usr/local/Ascend/ascend-toolkit/latest/tools/hccl_test
if [ $? -ne 0 ]; then
    echo "Failed to change directory to hccl_test."
    exit 1

fi
source /usr/local/Ascend/ascend-toolkit/latest/bin/setenv.bash
if [ $? -ne 0 ]; then
    echo "Failed to source setenv.bash."
    exit 1

fi
make MPI_HOME=/home/hjh/mpich ASCEND_DIR=/usr/local/Ascend/ascend-toolkit/latest
if [ $? -ne 0 ]; then
    echo "Failede to make hccl_test."
    exit 1

fi 
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/Ascend/ascend-toolkit/latest/lib64:/home/hjh/mpich/lib
if [ $? -ne 0 ]; then
    echo "Failed to export LD_LIBRARY_PATH."
    exit 1

fi

HOSTFILE="hostfile" B="8" E="4G" O=(sum prod) F="2" D=(fp16 fp32) P="2"
date
start=$(date +%s)

echo "smoke start time:"${start}

/home/hjh/mpich/bin/mpirun -n 2 -f $HOSTFILE ./bin/all_reduce_test -b $B -e 32M -f $F -o ${O[0]} -d ${D[0]} -p $P | tee -a ${cur_path}/output/log/allreduce_sum_32M.txt
check_result ${cur_path}/output/log/allreduce_sum_32M.txt all_reduce_test


/home/hjh/mpich/bin/mpirun -n 2 -f $HOSTFILE ./bin/all_reduce_test -b $B -e 500M -f $F -o ${O[0]} -d ${D[0]} -p $P | tee -a ${cur_path}/output/log/allreduce_sum_500M.txt
check_result ${cur_path}/output/log/allreduce_sum_500M.txt all_reduce_test

/home/hjh/mpich/bin/mpirun -n 2 -f $HOSTFILE ./bin/all_reduce_test -b $B -e 32M -f $F -o ${O[1]} -d ${D[0]} -p $P | tee -a ${cur_path}/output/log/allreduce_prod_32M.txt
check_result ${cur_path}/output/log/allreduce_prod_32M.txt all_reduce_test

/home/hjh/mpich/bin/mpirun -n 2 -f $HOSTFILE ./bin/reduce_test -b $B -e 512 -f $F -o ${O[0]} -d ${D[1]} -p $P | tee -a ${cur_path}/output/log/reduce_sum_512.txt
check_result ${cur_path}/output/log/reduce_sum_512.txt reduce_test

/home/hjh/mpich/bin/mpirun -n 2 -f $HOSTFILE ./bin/reduce_scatter_test -b $B -e 4M -f $F -o ${O[1]} -d ${D[1]} -p $P | tee -a ${cur_path}/output/log/reducescatter_prod_4M.txt
check_result ${cur_path}/output/log/reducescatter_prod_4M.txt reduce_scatter_test

/home/hjh/mpich/bin/mpirun -n 2 -f $HOSTFILE ./bin/all_gather_test -b $B -e 4M -f $F -d ${D[1]} -p $P | tee -a ${cur_path}/output/log/all_gather.txt
check_result ${cur_path}/output/log/all_gather.txt all_gather_test

/home/hjh/mpich/bin/mpirun -n 2 -f $HOSTFILE ./bin/scatter_test -b $B -e 4M -f $F -d ${D[1]} -p $P | tee -a ${cur_path}/output/log/scatter.txt
check_result ${cur_path}/output/log/scatter.txt scatter_test

/home/hjh/mpich/bin/mpirun -n 2 -f $HOSTFILE ./bin/alltoallv_test -b 128M -e 256M -f $F -d ${D[1]} -p $P | tee -a ${cur_path}/output/log/alltoallv.txt
check_result ${cur_path}/output/log/alltoallv.txt alltoallv_test

echo "end $flag"
end=$(date +%s)
diff=$[end - start]
date
echo "count time: ${diff}s"
echo "end time: $(date)"

if [ "${flag}" = 8 ]
then 
	echo "test result: pass " | tee -a ${cur_path}/output/result/result.txt
else
	echo "test result: fail " |tee -a ${cur_path}/output/result/result.txt

fi
cp ${cur_path}/output/result/result.txt ${cur_path}/result.txt
